package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.Bargaininfo;
import com.qf.enums.CustomerEnum;
import com.qf.bargainexception.PoorException;
import com.qf.mapper.BargainMapper;
import com.qf.service.BargainService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BargainServiceImpl implements BargainService {

    @Autowired
    private BargainMapper bargainMapper;

    @Override
    public PageInfo<Bargaininfo> findAll(Integer page,Integer size,String charityname,Integer styleid,Integer checkstatus) {
        PageHelper.startPage(page,size);
        Map<String,Object> map=new HashMap<>();
        map.put("charityname",charityname);
        map.put("styleid",styleid);
        map.put("checkstatus",checkstatus);
        List<Bargaininfo> bargainList=bargainMapper.findAll(map);
        PageInfo<Bargaininfo> pageInfo = new PageInfo<>(bargainList);
        return pageInfo;
    }

    @Override
    public Bargaininfo findOne(Integer id) {
        Bargaininfo bargaininfo=bargainMapper.findOne(id);
        return bargaininfo;
    }

    @Override
    public Integer findCountByName(String bargainname) {
        return bargainMapper.findCountByName(bargainname);
    }

    @Override
    @Transactional
    public void addBargain(Bargaininfo bargaininfo) {
        //设置默认状态为1,
        bargaininfo.setStatus(1);
        int count = bargainMapper.insertSelective(bargaininfo);
        if(count!=1){
            throw new PoorException(CustomerEnum.Bargain_ADD_ERROR);
        }
    }

    @Override
    @Transactional
    public void updateBargain(Bargaininfo bargaininfo) {
        int count = bargainMapper.updateByPrimaryKeySelective(bargaininfo);
        if(count!=1){
            throw new PoorException(CustomerEnum.Bargain_UPDATE_ERROR);
        }
    }

    @Override
    public Integer findCountById(String bargainname, Integer id) {
        Map<String,Object> map=new HashMap<>();
        map.put("bargainname",bargainname);
        map.put("id",id);
        return bargainMapper.findCountById(map);
    }

    //假删除（修改状态）
    @Override
    @Transactional
    public Integer deleteById(Integer id, Integer delete) {
        Bargaininfo bargaininfo=new Bargaininfo();
        bargaininfo.setId(id);
        bargaininfo.setStatus(delete);
        return bargainMapper.updateByPrimaryKeySelective(bargaininfo);
    }

    @Override
    public Integer deleteInIds(List<Integer> bargainidList) {
        return bargainMapper.deleteInIds(bargainidList);
    }

    @Override
    public List<Bargaininfo> findBargainByIds(Integer[] bargainid) {
        return bargainMapper.findBargainByIds(bargainid);
    }

    //查询明天到期合同的id
    @Override
    public List<Integer> findDueIds() {
        return bargainMapper.findDueIds();
    }

    @Override
    public Bargaininfo findOneById(Integer id) {
        return bargainMapper.selectByPrimaryKey(id);
    }


}
